//
//  DateTurnStr.m
//  CardManage
//
//  Created by C on 2017/11/7.
//  Copyright © 2017年 mupay. All rights reserved.
//

#import "DateTurnStr.h"
@implementation DateTurnStr

+ (NSString *)StringChangeNSdate:(NSString *)dateStr andType:(BOOL)needHMS{
    dateStr = [dateStr substringWithRange:NSMakeRange(0, 10)];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    if(needHMS){
        [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    }else{
        [formatter setDateFormat:@"YYYY-MM-dd"];
    }
    NSDate *confromTimesp =  [[NSDate alloc]initWithTimeIntervalSince1970:[dateStr longLongValue]];;
    NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];
    if([dateStr isEqualToString:@""]||[dateStr isKindOfClass:[NSNull class]] || dateStr == nil){
        return @"";
    }
    return confromTimespStr;
}

+ (NSString *)dateTurnString:(NSString *)str{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
//    formatter.timeZone = [NSTimeZone systemTimeZone];
    NSDate* date = [formatter dateFromString:str];
    NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[date timeIntervalSince1970]*1000];
    //    NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[dateNow timeIntervalSince1970]];
    return timeSp;
}

+(NSInteger)CompareDate:(NSDate *)startDate :(NSDate *)endDate
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterNoStyle];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSTimeZone* GTMzone = [NSTimeZone systemTimeZone];;
    [formatter setTimeZone:GTMzone];
    
    NSString *timeStr = [formatter stringFromDate:startDate];
    NSString *NowStr = [formatter stringFromDate:endDate];
    NSDate   *StartDate = [formatter dateFromString:timeStr];
    NSInteger interval = [GTMzone secondsFromGMTForDate:StartDate];
    StartDate = [StartDate dateByAddingTimeInterval: interval];
    NSDate   *EndDate = [formatter dateFromString:NowStr];
    EndDate = [EndDate dateByAddingTimeInterval: interval];
    
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSInteger unitFlags = NSCalendarUnitDay;
    NSDateComponents *comps = [gregorian components:unitFlags fromDate:StartDate  toDate:EndDate  options:0];
    NSInteger days = [comps day];
    NSLog(@"第一个时间:%@，第二个时间：%@",StartDate,EndDate);
    NSLog(@"相差多少天:%ld",(long)days);
    return days;
}

//账单日和开始时间对比
+(NSInteger)CompareStatementDate:(NSString *)StatementDate :(NSString *)RepaymentDate :(NSDate *)StartDate{
    
    NSString  *DayStr;
    NSDate *NowDate=[NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterNoStyle];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSTimeZone* GTMzone = [NSTimeZone systemTimeZone];
    [formatter setTimeZone:GTMzone];
    
    NSDateFormatter *Yearformatter = [[NSDateFormatter alloc] init];
    [Yearformatter setDateFormat:@"yyyy"];
    [Yearformatter setTimeZone:GTMzone];
    NSDateFormatter *Monthformatter = [[NSDateFormatter alloc] init];
    [Monthformatter setDateFormat:@"MM"];
    [Monthformatter setTimeZone:GTMzone];
    NSDateFormatter *Dayformatter = [[NSDateFormatter alloc] init];
    [Dayformatter setDateFormat:@"dd"];
    [Dayformatter setTimeZone:GTMzone];
    
    int Today = [[Dayformatter stringFromDate:NowDate] intValue];
    int Month;
    int Year;
//    if ([StatementDate intValue] > [RepaymentDate intValue]) {//
//        Month = [[Monthformatter stringFromDate:NowDate] intValue];
//    }else{
//
//    }
    if ([StatementDate intValue] > [RepaymentDate intValue] && Today < [StatementDate intValue]&& Today < [RepaymentDate intValue]) {
        Month = [[Monthformatter stringFromDate:NowDate] intValue] -1;
    }
    else if ([StatementDate intValue] < Today && [RepaymentDate intValue] <Today && [StatementDate intValue] < [RepaymentDate intValue]){
       Month = [[Monthformatter stringFromDate:NowDate] intValue] +1 ;
    }
    else{
       Month = [[Monthformatter stringFromDate:NowDate] intValue];
    }
    if (Month>12) {
        Year =[[Yearformatter stringFromDate:NowDate] intValue]+1;
        Month = 01;
    }else{
        Year =[[Yearformatter stringFromDate:NowDate] intValue];
    }
    if ([StatementDate intValue]<10) {
        DayStr = [NSString stringWithFormat:@"%d-%d-0%@",Year,Month,StatementDate];
    }else{
        DayStr = [NSString stringWithFormat:@"%d-%d-%@",Year,Month,StatementDate];
    }
    
    NSDate   *NowDay = [formatter dateFromString:DayStr];
    NSString *StartDay = [formatter stringFromDate:StartDate];
    
    NSInteger interval = [GTMzone secondsFromGMTForDate:NowDay];
    NowDay = [NowDay dateByAddingTimeInterval: interval];
    NSDate   *choseDate = [formatter dateFromString:StartDay];
    choseDate = [choseDate dateByAddingTimeInterval: interval];
    
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSInteger unitFlags = NSCalendarUnitDay;
    NSDateComponents *comps = [gregorian components:unitFlags fromDate:NowDay  toDate:choseDate  options:0];
    NSInteger days = [comps day];
    NSLog(@"选择时间:%@，账单日：%@",choseDate,NowDay);
    NSLog(@"相差多少天%ldd",(long)days);
    return days;
    
}

//还款日和结束时间对比
+(NSInteger)CompareRepaymentDate:(NSString *)RepaymentDate :(NSString *)StatementDate :(NSDate *)EndDate{
    
    NSString  *DayStr;
    NSDate *NowDate=[NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterNoStyle];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSTimeZone* GTMzone = [NSTimeZone systemTimeZone];
    [formatter setTimeZone:GTMzone];
    
    NSDateFormatter *Yearformatter = [[NSDateFormatter alloc] init];
    [Yearformatter setDateFormat:@"yyyy"];
    [Yearformatter setTimeZone:GTMzone];
    NSDateFormatter *Monthformatter = [[NSDateFormatter alloc] init];
    [Monthformatter setDateFormat:@"MM"];
    [Monthformatter setTimeZone:GTMzone];
    NSDateFormatter *Dayformatter = [[NSDateFormatter alloc] init];
    [Dayformatter setDateFormat:@"dd"];
    [Dayformatter setTimeZone:GTMzone];
    
    int Today = [[Dayformatter stringFromDate:NowDate] intValue];
    int Month;
    int Year;
//#pragma mark ----year 需要考虑到跨年
    if ([StatementDate intValue] >= [RepaymentDate intValue] && Today >= [StatementDate intValue]) {
         Month = [[Monthformatter stringFromDate:NowDate] intValue]+1;
    }else if ([StatementDate intValue] < Today && Today > [RepaymentDate intValue]){
        Month = [[Monthformatter stringFromDate:NowDate] intValue] +1 ;
    }else if ([StatementDate intValue] >= [RepaymentDate intValue] && Today > [RepaymentDate intValue]){
        Month = [[Monthformatter stringFromDate:NowDate] intValue] +1 ;
    }else{
        Month = [[Monthformatter stringFromDate:NowDate] intValue];
    }
    if (Month>12) {
       Year =[[Yearformatter stringFromDate:NowDate] intValue]+1;
       Month = 01;
    }else{
       Year =[[Yearformatter stringFromDate:NowDate] intValue];
    }
    if ([RepaymentDate intValue]<10) {
      DayStr = [NSString stringWithFormat:@"%d-%d-0%@",Year,Month,RepaymentDate];
    }else{
      DayStr = [NSString stringWithFormat:@"%d-%d-%@",Year,Month,RepaymentDate];
    }

    NSDate   *NowDay = [formatter dateFromString:DayStr];
    NSString *EndDay = [formatter stringFromDate:EndDate];

    NSInteger interval = [GTMzone secondsFromGMTForDate:NowDay];
    NowDay = [NowDay dateByAddingTimeInterval: interval];
    NSDate   *choseDate = [formatter dateFromString:EndDay];
    choseDate = [choseDate dateByAddingTimeInterval: interval];
    
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSInteger unitFlags = NSCalendarUnitDay;
    NSDateComponents *comps = [gregorian components:unitFlags fromDate:choseDate  toDate:NowDay  options:0];
    NSInteger days = [comps day];
    NSLog(@"选择时间:%@，还款日：%@",choseDate,NowDay);
    NSLog(@"相差多少天:%ld",(long)days);
    return days;
}


+ (BOOL )CompareTime:(NSString *)planTime :(NSDate *)ChoseDate
{
    NSString  *DayStr;
    NSDate *NowDate=[NSDate date];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterNoStyle];
    [formatter setDateFormat:@"yyyy-MM-dd"];

    NSDateFormatter *ToDayformatter = [[NSDateFormatter alloc] init];
    [ToDayformatter setDateStyle:NSDateFormatterMediumStyle];
    [ToDayformatter setTimeStyle:NSDateFormatterNoStyle];
    [ToDayformatter setDateFormat:@"HH:mm:ss"];
    
    NSDateFormatter *Dayformatter = [[NSDateFormatter alloc] init];
    [Dayformatter setDateStyle:NSDateFormatterMediumStyle];
    [Dayformatter setTimeStyle:NSDateFormatterNoStyle];
    [Dayformatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    NSDateFormatter *DDformatter = [[NSDateFormatter alloc] init];
    [DDformatter setDateStyle:NSDateFormatterMediumStyle];
    [DDformatter setTimeStyle:NSDateFormatterNoStyle];
    [DDformatter setDateFormat:@"dd"];
 
    
    NSTimeZone* GTMzone = [NSTimeZone systemTimeZone];;
    [formatter setTimeZone:GTMzone];
    [ToDayformatter setTimeZone:GTMzone];
    [Dayformatter setTimeZone:GTMzone];
    [DDformatter setTimeZone:GTMzone];
    
    NSString *planTimeStr  = [ToDayformatter stringFromDate:NowDate];//获取当前时间的时分秒
    NSString *TimeStr = [formatter stringFromDate:NowDate];//获取当前时间的年月日
    NSString *TodayDate = [formatter stringFromDate:ChoseDate];//获取选择时间的年月日
    
    DayStr = [NSString stringWithFormat:@"%@ %@",TimeStr,planTime];
    NSString *ChoseStr = [NSString stringWithFormat:@"%@ %@",TodayDate,planTimeStr];
    
    NSDate *ChoseToday = [Dayformatter dateFromString:ChoseStr];
    NSDate *GetTimeDate = [Dayformatter dateFromString:DayStr];
    
    int Today = [[DDformatter stringFromDate:NowDate] intValue];
    int Choseday = [[DDformatter stringFromDate:ChoseDate] intValue];
    
    //注意这里，只有选择的时候是当天才去对比，因为这是判断提前的。选择后面的时间无论如何都能通过
    if (Today == Choseday) {
        NSComparisonResult result = [ChoseToday compare:GetTimeDate];
        switch (result)
        {
                //date02比date01大
            case NSOrderedAscending:
                return true;
                break;
                //date02比date01小
            case NSOrderedDescending:
                return false;
                break;
                //date02=date01
            case NSOrderedSame:
                return true;
                break;
            default: NSLog(@"dates %@, %@", TodayDate, GetTimeDate);
                break;
        }

    }else{
        return true;
    }
}
@end
